package com.zzjson.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.config</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月28日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@Configuration
public class RabbitConfig {
	//####################定义四个队列####################
	@Bean
	@Qualifier("firstQueue")
	public Queue getFirstQueue() {
		return new Queue("boot-first-queue");
	}

	@Bean
	@Qualifier("secondQueue")
	public Queue getSecondQueue() {
		return new Queue("boot-second-queue");
	}

	@Bean
	@Qualifier("thirdQueue")
	public Queue getThirdQueue() {
		return new Queue("boot-third-queue");
	}

	@Bean
	@Qualifier("fourthQueue")
	public Queue getFourthQueue() {
		return new Queue("boot-fourth-queue");
	}

	//####################创建三个交换机####################
	@Bean
	@Qualifier("directExchange")
	public DirectExchange getDirectExchange() {
		return new DirectExchange("boot-direct-exchange");
	}

	@Bean
	@Qualifier("topicExchange")
	public TopicExchange getTopicExchange() {
		return new TopicExchange("boot-topic-exchange");
	}

	@Bean
	@Qualifier("fanoutExchange")
	public FanoutExchange getFanoutExchange() {
		return new FanoutExchange("boot-fanout-exchange");
	}

	//####################定义四个绑定关系####################
	@Bean
	public Binding bindFirst(@Qualifier("firstQueue") Queue queue,
							 @Qualifier("directExchange") DirectExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("test");
	}

	@Bean
	public Binding bindSecond(@Qualifier("secondQueue") Queue queue,
							  @Qualifier("topicExchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("*.zzy.*");
	}

	@Bean
	public Binding bindThird(@Qualifier("thirdQueue") Queue queue,
							 @Qualifier("fanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}


	@Bean
	public Binding bindFourth(@Qualifier("fourthQueue") Queue queue,
							  @Qualifier("fanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}

	/**
	 * 在消费端转换JSON消息
	 * 监听类都要加上containerFactory属性
	 *
	 * @param connectionFactory
	 * @return
	 */
	@Bean
	public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
		SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
		factory.setConnectionFactory(connectionFactory);
		factory.setConcurrentConsumers(3);
		factory.setMaxConcurrentConsumers(10);
		factory.setMessageConverter(new Jackson2JsonMessageConverter());
		factory.setAutoStartup(true);
		factory.setAcknowledgeMode(AcknowledgeMode.NONE);
		return factory;
	}
}